using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using Decav.Reality;
using System.IO;
using Decav.Gatsb.Mobile;

namespace Decav.Gatsb
{
    /// <summary>
    /// Defines and provides telephone service provider specific methods and features.
    /// </summary>
    [Serializable]
    public abstract class TelephoneServiceProvider
    {
        public TelephoneServiceProvider(string name)
        {
            if (name == null)
                throw new ArgumentNullException("name");

            _name = name;
        }

        private string _name;

        /// <summary>
        /// Gets the display name of the telephone provider.
        /// </summary>
        public string Name
        {
            get
            {
                return _name;
            }
        }

        /// <summary>
        /// Creates the Short-Messaging-Service email address for the given telephone number.
        /// </summary>
        /// <param name="phoneNumber">The phone number to get the address for.</param>
        /// <returns>The mail address to send SMS messages to.</returns>
        public abstract EmailAddress CreateSmsAddress(PhoneNumber phoneNumber);

        /// <summary>
        /// Creates the Media-Messaging-Service email address for the given telephone number.
        /// </summary>
        /// <param name="phoneNumber">The phone number to get the address for.</param>
        /// <returns>The mail address to send MMS messages to.</returns>
        public abstract EmailAddress CreateMmsAddress(PhoneNumber phoneNumber);

        /// <summary>
        /// Gets whether or not the specified address is an SMS or MMS address issued by this service provider.
        /// </summary>
        /// <param name="mailAddress">The address to parse.</param>
        /// <returns>True when the address is for this provider, else false.</returns>
        public abstract bool IsProviderMailAddress(string mailAddress);

        /// <summary>
        /// Creates a parser to parse a mail message for the <see cref="TelephoneServiceProvider"/>.
        /// </summary>
        /// <returns>The message parser to use when parsing messages for this mobile provider.</returns>
        public abstract IPhoneMessageParser CreateMessageParser();

        /// <summary>
        /// Gets the name of the type that that this provider is, that is saved to the database and other locations.
        /// </summary>
        /// <returns>The name of the type.</returns>
        public string GetTypeString()
        {
            Type telProviderType = this.GetType();
            return string.Format("{0},{1}", telProviderType.FullName,
                            Path.GetFileNameWithoutExtension(telProviderType.Assembly.ManifestModule.Name));
        }

        #region Static

        /// <summary>
        /// Gets a telephone service provider given its type name.
        /// </summary>
        /// <param name="type">The name of the service.</param>
        /// <returns>The telephone service provider.</returns>
        public static TelephoneServiceProvider GetProvider(string type)
        {
            if (type == null)
                throw new ArgumentNullException("type");

            return (TelephoneServiceProvider)Activator.CreateInstance(Type.GetType(type, true));
        }

        #endregion
    }
}
